define(["core/boilerplate", "core/manager", "tile", "conf/tile"], 
	function(Boilerplate, Manager, Tile, TileConf) {
		var TileManager = new Manager(Tile);

		// Override initialization
		TileManager._init = TileManager.init;
		TileManager.init = function() {

			// Get number of tiles needed
			var size = TileConf.def.size;
			var cHorizontal = Boilerplate.scene.width/size;
			var cVertical = Boilerplate.scene.height/size;
			var c = cHorizontal * cVertical;

			// Call parent initialize for creating tiles
			TileManager._init(c, TileConf);

			// Initialize position
			var x = 0;
			var y = 0;

			// Set position for each tile
			for(var iE = 0; iE < c; iE++) {

				this.entities[iE].configure({
					id : x+"."+y,
					x : x,
					y : y,
					size : size
				});

				// Increment position
				x += size;
				// Reach max width ?
				if(iE > 0 && iE%cHorizontal === 0) {
					// Go to the next row
					x = 0;
					y += size;
				}
			}

			this.doSeed();
		}

		TileManager.doSeed = function() {
			var count = 0;
			var texture = TileConf.textures[5];
			// Plant grass seed
			for(var iE = 0, c = this.entities.length; iE < c; iE++) {
				var tile = this.entities[iE];
				var kind = tile.texture.kind;

				// Current tile is a grass seed
				if(kind === texture.kind) {
					// Get sibling tiles
					var prev = iE > 0 ? this.entities[iE-1] : null;
					var next = iE < c-1 ? this.entities[iE+1] : null;

					// If previous/next tile exist but are not the same kind of tile
					if(prev !== null && prev.texture.kind !== kind && 
						next !== null && next.texture.kind !== kind
					) {
						// Reset current tile
						tile.texture = TileConf.textures[0];
						count++;
					}
				}
			}
			log("Tile reset : " + count);
		}

		return TileManager;
	}
);